2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
8 include_once( "CacheManager.php" );
11 /* private */ var $mContent, $mContentLoaded;
12 /* private */ var $mUser, $mTimestamp, $mUserText;
13 /* private */ var $mCounter, $mComment, $mCountAdjustment;
14 /* private */ var $mMinorEdit, $mRedirectedFrom;
15 /* private */ var $mTouched, $mFileCache;
17 function Article() { $this->clear(); }
19 /* private */ function clear()
21 $this->mContentLoaded
= false;
22 $this->mUser
= $this->mCounter
= -1; # Not loaded
23 $this->mRedirectedFrom
= $this->mUserText
=
24 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
25 $this->mCountAdjustment
= 0;
26 $this->mTouched
= "19700101000000";
29 /* static */ function newFromID( $newid )
31 global $wgOut, $wgTitle, $wgArticle;
33 $n = Article
::nameOf( $newid );
35 $wgTitle = Title
::newFromDBkey( $n );
36 $wgTitle->resetArticleID( $newid );
41 /* static */ function nameOf( $id )
43 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
45 $res = wfQuery( $sql, "Article::nameOf" );
46 if ( 0 == wfNumRows( $res ) ) { return NULL; }
48 $s = wfFetchObject( $res );
49 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
53 # Note that getContent/loadContent may follow redirects if
54 # not told otherwise, and so may cause a change to wgTitle.
56 function getContent( $noredir = false )
58 global $action,$section,$count,$wgTitle; # From query string
59 wfProfileIn( "Article::getContent" );
61 if ( 0 == $this->getID() ) {
62 if ( "edit" == $action ) {
65 return ""; # was "newarticletext", now moved above the box)
70 return wfMsg( "noarticletext" );
72 $this->loadContent( $noredir );
76 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
77 ( $wgTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
78 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$wgTitle->getText()) &&
82 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
86 if($section=="new") { return ""; }
88 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
90 PREG_SPLIT_DELIM_CAPTURE
);
92 return trim($secs[0]);
94 return trim($secs[$section*2-1] . $secs[$section*2]);
98 return $this->mContent
;
103 function loadContent( $noredir = false )
105 global $wgOut, $wgTitle, $wgMwRedir;
106 global $oldid, $redirect; # From query
108 if ( $this->mContentLoaded
) return;
109 $fname = "Article::loadContent";
111 # Pre-fill content with error message so that if something
112 # fails we'll have something telling us what we intended.
114 $t = $wgTitle->getPrefixedText();
115 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
116 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
117 $this->mContent
= str_replace( "$1", $t, wfMsg( "missingarticle" ) );
119 if ( ! $oldid ) { # Retrieve current version
120 $id = $this->getID();
121 if ( 0 == $id ) return;
124 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
125 "FROM cur WHERE cur_id={$id}";
126 $res = wfQuery( $sql, $fname );
127 if ( 0 == wfNumRows( $res ) ) { return; }
129 $s = wfFetchObject( $res );
131 # If we got a redirect, follow it (unless we've been told
132 # not to by either the function parameter or the query
133 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
134 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
135 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
136 $s->cur_text
, $m ) ) {
137 $rt = Title
::newFromText( $m[1] );
139 # Gotta hand redirects to special pages differently:
140 # Fill the HTTP response "Location" header and ignore
141 # the rest of the page we're on.
143 if ( $rt->getInterwiki() != "" ) {
144 $wgOut->redirect( $rt->getFullURL() ) ;
147 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
148 $wgOut->redirect( wfLocalUrl(
149 $rt->getPrefixedURL() ) );
152 $rid = $rt->getArticleID();
154 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
155 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
156 $res = wfQuery( $sql, $fname );
158 if ( 0 != wfNumRows( $res ) ) {
159 $this->mRedirectedFrom
= $wgTitle->getPrefixedText();
161 $s = wfFetchObject( $res );
166 $this->mContent
= $s->cur_text
;
167 $this->mUser
= $s->cur_user
;
168 $this->mCounter
= $s->cur_counter
;
169 $this->mTimestamp
= $s->cur_timestamp
;
170 $this->mTouched
= $s->cur_touched
;
171 $wgTitle->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
172 $wgTitle->mRestrictionsLoaded
= true;
173 wfFreeResult( $res );
174 } else { # oldid set, retrieve historical version
175 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
176 "WHERE old_id={$oldid}";
177 $res = wfQuery( $sql, $fname );
178 if ( 0 == wfNumRows( $res ) ) { return; }
180 $s = wfFetchObject( $res );
181 $this->mContent
= $s->old_text
;
182 $this->mUser
= $s->old_user
;
184 $this->mTimestamp
= $s->old_timestamp
;
185 wfFreeResult( $res );
187 $this->mContentLoaded
= true;
190 function getID() { global $wgTitle; return $wgTitle->getArticleID(); }
194 if ( -1 == $this->mCounter
) {
195 $id = $this->getID();
196 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
198 return $this->mCounter
;
201 # Would the given text make this article a "good" article (i.e.,
202 # suitable for including in the article count)?
204 function isCountable( $text )
206 global $wgTitle, $wgUseCommaCount, $wgMwRedir;
208 if ( 0 != $wgTitle->getNamespace() ) { return 0; }
209 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
210 $token = ($wgUseCommaCount ?
"," : "[[" );
211 if ( false === strstr( $text, $token ) ) { return 0; }
215 # Load the field related to the last edit time of the article.
216 # This isn't necessary for all uses, so it's only done if needed.
218 /* private */ function loadLastEdit()
221 if ( -1 != $this->mUser
) return;
223 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
224 "cur_comment,cur_minor_edit FROM cur WHERE " .
225 "cur_id=" . $this->getID();
226 $res = wfQuery( $sql, "Article::loadLastEdit" );
228 if ( wfNumRows( $res ) > 0 ) {
229 $s = wfFetchObject( $res );
230 $this->mUser
= $s->cur_user
;
231 $this->mUserText
= $s->cur_user_text
;
232 $this->mTimestamp
= $s->cur_timestamp
;
233 $this->mComment
= $s->cur_comment
;
234 $this->mMinorEdit
= $s->cur_minor_edit
;
238 function getTimestamp()
240 $this->loadLastEdit();
241 return $this->mTimestamp
;
246 $this->loadLastEdit();
250 function getUserText()
252 $this->loadLastEdit();
253 return $this->mUserText
;
256 function getComment()
258 $this->loadLastEdit();
259 return $this->mComment
;
262 function getMinorEdit()
264 $this->loadLastEdit();
265 return $this->mMinorEdit
;
268 # This is the default action of the script: just view the page of
273 global $wgUser, $wgOut, $wgTitle, $wgLang;
274 global $oldid, $diff; # From query
276 wfProfileIn( "Article::view" );
278 $wgOut->setArticleFlag( true );
279 $wgOut->setRobotpolicy( "index,follow" );
281 # If we got diff and oldid in the query, we want to see a
282 # diff page instead of the article.
284 if ( isset( $diff ) ) {
285 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
286 $de = new DifferenceEngine( $oldid, $diff );
291 $text = $this->getContent(); # May change wgTitle!
292 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
293 $wgOut->setHTMLTitle( $wgTitle->getPrefixedText() .
294 " - " . wfMsg( "wikititlesuffix" ) );
296 # We're looking at an old revision
299 $this->setOldSubtitle();
300 $wgOut->setRobotpolicy( "noindex,follow" );
302 if ( "" != $this->mRedirectedFrom
) {
303 $sk = $wgUser->getSkin();
304 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
306 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
307 $wgOut->setSubtitle( $s );
309 $wgOut->checkLastModified( $this->mTouched
);
310 $this->tryFileCache();
311 $wgLinkCache->preFill( $wgTitle );
312 $wgOut->addWikiText( $text );
314 # If the article we've just shown is in the "Image" namespace,
315 # follow it with the history list and link list for the image
318 if ( Namespace::getImage() == $wgTitle->getNamespace() ) {
319 $this->imageHistory();
322 $this->viewUpdates();
326 # Theoretically we could defer these whole insert and update
327 # functions for after display, but that's taking a big leap
328 # of faith, and we want to be able to report database
329 # errors at some point.
331 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
333 global $wgOut, $wgUser, $wgTitle, $wgLinkCache, $wgMwRedir;
334 $fname = "Article::insertNewArticle";
336 $ns = $wgTitle->getNamespace();
337 $ttl = $wgTitle->getDBkey();
338 $text = $this->preSaveTransform( $text );
339 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
342 $now = wfTimestampNow();
343 $won = wfInvertTimestamp( $now );
345 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
346 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
347 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
348 "cur_restrictions,cur_user_text,cur_is_redirect," .
349 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
350 wfStrencode( $text ) . "', '" .
351 wfStrencode( $summary ) . "', '" .
352 $wgUser->getID() . "', '{$now}', " .
353 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
354 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
355 $res = wfQuery( $sql, $fname );
357 $newid = wfInsertId();
358 $wgTitle->resetArticleID( $newid );
360 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
361 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
362 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
363 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
364 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
365 wfStrencode( $wgUser->getName() ) . "','" .
366 wfStrencode( $summary ) . "',0,0," .
367 ( $wgUser->isBot() ?
1 : 0 ) . ")";
368 wfQuery( $sql, $fname );
370 if(!$wgTitle->userIsWatching()) $this->watch();
372 if ( $wgTitle->userIsWatching() ) {
377 $this->showArticle( $text, wfMsg( "newarticle" ) );
380 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
382 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
383 global $wgDBtransactions, $wgMwRedir;
384 $fname = "Article::updateArticle";
386 $this->loadLastEdit();
388 // insert updated section into old text if we have only edited part
390 if ($section != "") {
391 $oldtext=$this->getContent();
392 if($section=="new") {
393 if($summary) $subject="== {$summary} ==\n\n";
394 $text=$oldtext."\n\n".$subject.$text;
396 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
397 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
398 $secs[$section*2]=$text."\n\n"; // replace with edited
399 if($section) { $secs[$section*2-1]=""; } // erase old headline
400 $text=join("",$secs);
403 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
404 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
405 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
407 $text = $m[1] . "\n"; # Remove all content but redirect
411 $text = $this->preSaveTransform( $text );
413 # Update article, but only if changed.
415 if( $wgDBtransactions ) {
419 $oldtext = $this->getContent( true );
421 if ( 0 != strcmp( $text, $oldtext ) ) {
422 $this->mCountAdjustment
= $this->isCountable( $text )
423 - $this->isCountable( $oldtext );
425 $now = wfTimestampNow();
426 $won = wfInvertTimestamp( $now );
427 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
428 "',cur_comment='" . wfStrencode( $summary ) .
429 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
430 ",cur_timestamp='{$now}',cur_user_text='" .
431 wfStrencode( $wgUser->getName() ) .
432 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
433 "WHERE cur_id=" . $this->getID() .
434 " AND cur_timestamp='" . $this->getTimestamp() . "'";
435 $res = wfQuery( $sql, $fname );
437 if( wfAffectedRows() == 0 ) {
438 /* Belated edit conflict! Run away!! */
442 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
443 "old_comment,old_user,old_user_text,old_timestamp," .
444 "old_minor_edit,inverse_timestamp) VALUES (" .
445 $wgTitle->getNamespace() . ", '" .
446 wfStrencode( $wgTitle->getDBkey() ) . "', '" .
447 wfStrencode( $oldtext ) . "', '" .
448 wfStrencode( $this->getComment() ) . "', " .
449 $this->getUser() . ", '" .
450 wfStrencode( $this->getUserText() ) . "', '" .
451 $this->getTimestamp() . "', " . $me1 . ", '" .
452 wfInvertTimestamp( $this->getTimestamp() ) . "')";
453 $res = wfQuery( $sql, $fname );
454 $oldid = wfInsertID( $res );
456 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
457 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
458 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
459 "'{$now}','{$now}'," . $wgTitle->getNamespace() . ",'" .
460 wfStrencode( $wgTitle->getDBkey() ) . "',0,{$me2}," .
461 ( $wgUser->isBot() ?
1 : 0 ) . "," .
462 $this->getID() . "," . $wgUser->getID() . ",'" .
463 wfStrencode( $wgUser->getName() ) . "','" .
464 wfStrencode( $summary ) . "',0,{$oldid})";
465 wfQuery( $sql, $fname );
467 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
468 "WHERE rc_namespace=" . $wgTitle->getNamespace() . " AND " .
469 "rc_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' AND " .
470 "rc_timestamp='" . $this->getTimestamp() . "'";
471 wfQuery( $sql, $fname );
473 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
474 "WHERE rc_cur_id=" . $this->getID();
475 wfQuery( $sql, $fname );
477 if( $wgDBtransactions ) {
483 if (!$wgTitle->userIsWatching()) $this->watch();
485 if ( $wgTitle->userIsWatching() ) {
490 $this->showArticle( $text, wfMsg( "updated" ) );
494 # After we've either updated or inserted the article, update
495 # the link tables and redirect to the new page.
497 function showArticle( $text, $subtitle )
499 global $wgOut, $wgTitle, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
502 $wgLinkCache = new LinkCache();
504 # Get old version of link table to allow incremental link updates
505 if ( $wgUseBetterLinksUpdate ) {
506 $wgLinkCache->preFill( $wgTitle );
507 $wgLinkCache->clear();
510 # Now update the link cache by parsing the text
511 $wgOut->addWikiText( $text );
513 $this->editUpdates( $text );
514 if( $wgMwRedir->matchStart( $text ) )
518 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL(), $r ) );
521 # If the page we've just displayed is in the "Image" namespace,
522 # we follow it with an upload history of the image and its usage.
524 function imageHistory()
526 global $wgUser, $wgOut, $wgLang, $wgTitle;
527 $fname = "Article::imageHistory";
529 $sql = "SELECT img_size,img_description,img_user," .
530 "img_user_text,img_timestamp FROM image WHERE " .
531 "img_name='" . wfStrencode( $wgTitle->getDBkey() ) . "'";
532 $res = wfQuery( $sql, $fname );
534 if ( 0 == wfNumRows( $res ) ) { return; }
536 $sk = $wgUser->getSkin();
537 $s = $sk->beginImageHistoryList();
539 $line = wfFetchObject( $res );
540 $s .= $sk->imageHistoryLine( true, $line->img_timestamp
,
541 $wgTitle->getText(), $line->img_user
,
542 $line->img_user_text
, $line->img_size
, $line->img_description
);
544 $sql = "SELECT oi_size,oi_description,oi_user," .
545 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
546 "oi_name='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
547 "ORDER BY oi_timestamp DESC";
548 $res = wfQuery( $sql, $fname );
550 while ( $line = wfFetchObject( $res ) ) {
551 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp
,
552 $line->oi_archive_name
, $line->oi_user
,
553 $line->oi_user_text
, $line->oi_size
, $line->oi_description
);
555 $s .= $sk->endImageHistoryList();
556 $wgOut->addHTML( $s );
559 function imageLinks()
561 global $wgUser, $wgOut, $wgTitle;
563 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
565 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
566 wfStrencode( $wgTitle->getDBkey() ) . "'";
567 $res = wfQuery( $sql, "Article::imageLinks" );
569 if ( 0 == wfNumRows( $res ) ) {
570 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
573 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
575 $sk = $wgUser->getSkin();
576 while ( $s = wfFetchObject( $res ) ) {
578 $link = $sk->makeKnownLink( $name, "" );
579 $wgOut->addHTML( "<li>{$link}</li>\n" );
581 $wgOut->addHTML( "</ul>\n" );
584 # Add this page to my watchlist
588 global $wgUser, $wgTitle, $wgOut, $wgLang;
589 global $wgDeferredUpdateList;
591 if ( 0 == $wgUser->getID() ) {
592 $wgOut->errorpage( "watchnologin", "watchnologintext" );
595 if ( wfReadOnly() ) {
596 $wgOut->readOnlyPage();
599 $wgUser->addWatch( $wgTitle );
601 $wgOut->setPagetitle( wfMsg( "addedwatch" ) );
602 $wgOut->setRobotpolicy( "noindex,follow" );
604 $sk = $wgUser->getSkin() ;
605 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
607 $text = str_replace( "$1", $link ,
608 wfMsg( "addedwatchtext" ) );
609 $wgOut->addHTML( $text );
611 $up = new UserUpdate();
612 array_push( $wgDeferredUpdateList, $up );
614 $wgOut->returnToMain( false );
619 global $wgUser, $wgTitle, $wgOut, $wgLang;
620 global $wgDeferredUpdateList;
622 if ( 0 == $wgUser->getID() ) {
623 $wgOut->errorpage( "watchnologin", "watchnologintext" );
626 if ( wfReadOnly() ) {
627 $wgOut->readOnlyPage();
630 $wgUser->removeWatch( $wgTitle );
632 $wgOut->setPagetitle( wfMsg( "removedwatch" ) );
633 $wgOut->setRobotpolicy( "noindex,follow" );
635 $sk = $wgUser->getSkin() ;
636 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
638 $text = str_replace( "$1", $link ,
639 wfMsg( "removedwatchtext" ) );
640 $wgOut->addHTML( $text );
642 $up = new UserUpdate();
643 array_push( $wgDeferredUpdateList, $up );
645 $wgOut->returnToMain( false );
648 # This shares a lot of issues (and code) with Recent Changes
652 global $wgUser, $wgOut, $wgLang, $wgTitle, $offset, $limit;
654 # If page hasn't changed, client can cache this
656 $wgOut->checkLastModified( $this->getTimestamp() );
657 wfProfileIn( "Article::history" );
659 $wgOut->setPageTitle( $wgTitle->getPRefixedText() );
660 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
661 $wgOut->setArticleFlag( false );
662 $wgOut->setRobotpolicy( "noindex,nofollow" );
664 if( $wgTitle->getArticleID() == 0 ) {
665 $wgOut->addHTML( wfMsg( "nohistory" ) );
670 $offset = (int)$offset;
671 $limit = (int)$limit;
672 if( $limit == 0 ) $limit = 50;
673 $namespace = $wgTitle->getNamespace();
674 $title = $wgTitle->getText();
675 $sql = "SELECT old_id,old_user," .
676 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
677 "FROM old USE INDEX (name_title_timestamp) " .
678 "WHERE old_namespace={$namespace} AND " .
679 "old_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
680 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
681 $res = wfQuery( $sql, "Article::history" );
683 $revs = wfNumRows( $res );
684 if( $wgTitle->getArticleID() == 0 ) {
685 $wgOut->addHTML( wfMsg( "nohistory" ) );
690 $sk = $wgUser->getSkin();
691 $numbar = wfViewPrevNext(
693 $wgTitle->getPrefixedText(),
696 $s .= $sk->beginHistoryList();
699 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
700 $this->getUserText(), $namespace,
701 $title, 0, $this->getComment(),
702 ( $this->getMinorEdit() > 0 ) );
704 $revs = wfNumRows( $res );
705 while ( $line = wfFetchObject( $res ) ) {
706 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
707 $line->old_user_text
, $namespace,
708 $title, $line->old_id
,
709 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
711 $s .= $sk->endHistoryList();
713 $wgOut->addHTML( $s );
719 global $wgUser, $wgOut, $wgTitle;
721 if ( ! $wgUser->isSysop() ) {
722 $wgOut->sysopRequired();
725 if ( wfReadOnly() ) {
726 $wgOut->readOnlyPage();
729 $id = $wgTitle->getArticleID();
731 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
734 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
735 "cur_restrictions='sysop' WHERE cur_id={$id}";
736 wfQuery( $sql, "Article::protect" );
738 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
743 global $wgUser, $wgOut, $wgTitle;
745 if ( ! $wgUser->isSysop() ) {
746 $wgOut->sysopRequired();
749 if ( wfReadOnly() ) {
750 $wgOut->readOnlyPage();
753 $id = $wgTitle->getArticleID();
755 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
758 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
759 "cur_restrictions='' WHERE cur_id={$id}";
760 wfQuery( $sql, "Article::unprotect" );
762 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
767 global $wgUser, $wgOut, $wgTitle;
768 global $wpConfirm, $wpReason, $image, $oldimage;
770 # Anybody can delete old revisions of images; only sysops
771 # can delete articles and current images
773 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
774 $wgOut->sysopRequired();
777 if ( wfReadOnly() ) {
778 $wgOut->readOnlyPage();
782 # Better double-check that it hasn't been deleted yet!
783 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
785 if ( "" == trim( $image ) ) {
786 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
789 $sub = str_replace( "$1", $image, wfMsg( "deletesub" ) );
792 if ( ( "" == trim( $wgTitle->getText() ) )
793 or ( $wgTitle->getArticleId() == 0 ) ) {
794 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
797 $sub = str_replace( "$1", $wgTitle->getPrefixedText(),
798 wfMsg( "deletesub" ) );
800 # determine whether this page has earlier revisions
801 # and insert a warning if it does
802 # we select the text because it might be useful below
803 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
804 $res=wfQuery($sql,$fname);
805 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
806 $skin=$wgUser->getSkin();
807 $wgOut->addHTML("<B>".wfMsg("historywarning"));
808 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
811 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."'";
812 $res=wfQuery($sql,$fname);
813 if( ($s=wfFetchObject($res))) {
815 # if this is a mini-text, we can paste part of it into the deletion reason
817 #if this is empty, an earlier revision may contain "useful" text
818 if($s->cur_text
!="") {
822 $text=$old->old_text
;
828 $length=strlen($text);
830 # this should not happen, since it is not possible to store an empty, new
831 # page. Let's insert a standard text in case it does, though
832 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
835 if($length < 500 && !$wpReason) {
837 # comment field=255, let's grep the first 150 to have some user
839 $text=substr($text,0,150);
840 # let's strip out newlines and HTML tags
841 $text=preg_replace("/\"/","'",$text);
842 $text=preg_replace("/\</","<",$text);
843 $text=preg_replace("/\>/",">",$text);
844 $text=preg_replace("/[\n\r]/","",$text);
846 $wpReason=wfMsg("excontent"). " '".$text;
848 $wpReason=wfMsg("exbeforeblank") . " '".$text;
850 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
857 # Likewise, deleting old images doesn't require confirmation
858 if ( $oldimage ||
1 == $wpConfirm ) {
863 $wgOut->setSubtitle( $sub );
864 $wgOut->setRobotpolicy( "noindex,nofollow" );
865 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
867 $t = $wgTitle->getPrefixedURL();
868 $q = "action=delete";
871 $q .= "&image={$image}";
872 } else if ( $oldimage ) {
873 $q .= "&oldimage={$oldimage}";
877 $formaction = wfEscapeHTML( wfLocalUrl( "", $q ) );
878 $confirm = wfMsg( "confirm" );
879 $check = wfMsg( "confirmcheck" );
880 $delcom = wfMsg( "deletecomment" );
883 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
884 <table border=0><tr><td align=right>
885 {$delcom}:</td><td align=left>
886 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
887 </td></tr><tr><td> </td></tr>
889 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
890 </td><td><label for=\"wpConfirm\">{$check}</label></td>
891 </tr><tr><td> </td><td>
892 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
893 </td></tr></table></form>\n" );
895 $wgOut->returnToMain( false );
900 global $wgOut, $wgTitle, $wgUser, $wgLang;
901 global $image, $oldimage, $wpReason;
902 $fname = "Article::doDelete";
905 $dest = wfImageDir( $image );
906 $archive = wfImageDir( $image );
907 if ( ! unlink( "{$dest}/{$image}" ) ) {
908 $wgOut->fileDeleteError( "{$dest}/{$image}" );
911 $sql = "DELETE FROM image WHERE img_name='" .
912 wfStrencode( $image ) . "'";
913 wfQuery( $sql, $fname );
915 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
916 wfStrencode( $image ) . "'";
917 $res = wfQuery( $sql, $fname );
919 while ( $s = wfFetchObject( $res ) ) {
920 $this->doDeleteOldImage( $s->oi_archive_name
);
922 $sql = "DELETE FROM oldimage WHERE oi_name='" .
923 wfStrencode( $image ) . "'";
924 wfQuery( $sql, $fname );
926 # Image itself is now gone, and database is cleaned.
927 # Now we remove the image description page.
929 $nt = Title
::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
930 $this->doDeleteArticle( $nt );
933 } else if ( $oldimage ) {
934 $this->doDeleteOldImage( $oldimage );
935 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
936 wfStrencode( $oldimage ) . "'";
937 wfQuery( $sql, $fname );
939 $deleted = $oldimage;
941 $this->doDeleteArticle( $wgTitle );
942 $deleted = $wgTitle->getPrefixedText();
944 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
945 $wgOut->setRobotpolicy( "noindex,nofollow" );
947 $sk = $wgUser->getSkin();
948 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
949 Namespace::getWikipedia() ) .
950 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
952 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
953 $text = str_replace( "$2", $loglink, $text );
955 $wgOut->addHTML( "<p>" . $text );
956 $wgOut->returnToMain( false );
959 function doDeleteOldImage( $oldimage )
963 $name = substr( $oldimage, 15 );
964 $archive = wfImageArchiveDir( $name );
965 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
966 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
970 function doDeleteArticle( $title )
972 global $wgUser, $wgOut, $wgLang, $wpReason, $wgTitle, $wgDeferredUpdateList;
974 $fname = "Article::doDeleteArticle";
975 $ns = $title->getNamespace();
976 $t = wfStrencode( $title->getDBkey() );
977 $id = $title->getArticleID();
980 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
984 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
985 array_push( $wgDeferredUpdateList, $u );
987 # Move article and history to the "archive" table
988 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
989 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
990 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
991 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
992 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
993 wfQuery( $sql, $fname );
995 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
996 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
997 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
998 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
999 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1000 wfQuery( $sql, $fname );
1002 # Now that it's safely backed up, delete it
1004 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1006 wfQuery( $sql, $fname );
1008 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1010 wfQuery( $sql, $fname );
1012 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1014 wfQuery( $sql, $fname );
1016 # Finally, clean up the link tables
1019 $t = wfStrencode( $title->getPrefixedDBkey() );
1020 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
1021 $res = wfQuery( $sql, $fname );
1023 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1024 $now = wfTimestampNow();
1025 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1028 while ( $s = wfFetchObject( $res ) ) {
1029 $nt = Title
::newFromDBkey( $s->l_from
);
1030 $lid = $nt->getArticleID();
1032 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1034 $sql .= "({$lid},'{$t}')";
1039 wfQuery( $sql, $fname );
1040 wfQuery( $sql2, $fname );
1042 wfFreeResult( $res );
1044 $sql = "DELETE FROM links WHERE l_to={$id}";
1045 wfQuery( $sql, $fname );
1047 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1048 wfQuery( $sql, $fname );
1050 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1051 wfQuery( $sql, $fname );
1053 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1054 wfQuery( $sql, $fname );
1057 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1058 $art = $title->getPrefixedText();
1059 $wpReason = wfCleanQueryVar( $wpReason );
1060 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
1062 # Clear the cached article id so the interface doesn't act like we exist
1063 $wgTitle->resetArticleID( 0 );
1064 $wgTitle->mArticleID
= 0;
1072 if ( strlen( $oldimage ) < 16 ) {
1073 $wgOut->unexpectedValueError( "oldimage", $oldimage );
1076 if ( wfReadOnly() ) {
1077 $wgOut->readOnlyPage();
1080 $name = substr( $oldimage, 15 );
1082 $dest = wfImageDir( $name );
1083 $archive = wfImageArchiveDir( $name );
1084 $curfile = "{$dest}/{$name}";
1086 if ( ! is_file( $curfile ) ) {
1087 $wgOut->fileNotFoundError( $curfile );
1090 $oldver = wfTimestampNow() . "!{$name}";
1091 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
1092 wfStrencode( $oldimage ) . "'" );
1094 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
1095 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
1098 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
1099 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
1101 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
1103 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1104 $wgOut->setRobotpolicy( "noindex,nofollow" );
1105 $wgOut->addHTML( wfMsg( "imagereverted" ) );
1106 $wgOut->returnToMain( false );
1111 global $wgUser, $wgTitle, $wgLang, $wgOut, $from;
1113 if ( ! $wgUser->isSysop() ) {
1114 $wgOut->sysopRequired();
1118 # Replace all this user's current edits with the next one down
1119 $tt = wfStrencode( $wgTitle->getDBKey() );
1120 $n = $wgTitle->getNamespace();
1122 # Get the last editor
1123 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1124 $res = wfQuery( $sql );
1125 if( ($x = wfNumRows( $res )) != 1 ) {
1127 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1130 $s = wfFetchObject( $res );
1131 $ut = wfStrencode( $s->cur_user_text
);
1132 $uid = $s->cur_user
;
1135 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1136 if( $from != $s->cur_user_text
) {
1137 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1138 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1139 htmlspecialchars( $wgTitle->getPrefixedText()),
1140 htmlspecialchars( $from ),
1141 htmlspecialchars( $s->cur_user_text
) ) );
1142 if($s->cur_comment
!= "") {
1144 wfMsg("editcomment",
1145 htmlspecialchars( $s->cur_comment
) ) );
1150 # Get the last edit not by this guy
1151 $sql = "SELECT old_text,old_user,old_user_text
1152 FROM old USE INDEX (name_title_timestamp)
1153 WHERE old_namespace={$n} AND old_title='{$tt}'
1154 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1155 ORDER BY inverse_timestamp LIMIT 1";
1156 $res = wfQuery( $sql );
1157 if( wfNumRows( $res ) != 1 ) {
1159 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1160 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1163 $s = wfFetchObject( $res );
1166 $newcomment = str_replace( "$1", $s->old_user_text
, wfMsg( "revertpage" ) );
1167 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1168 $wgOut->setRobotpolicy( "noindex,nofollow" );
1169 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1170 $this->updateArticle( $s->old_text
, $newcomment, 1, $wgTitle->userIsWatching() );
1172 $wgOut->returnToMain( false );
1176 # Do standard deferred updates after page view
1178 /* private */ function viewUpdates()
1180 global $wgDeferredUpdateList, $wgTitle;
1182 if ( 0 != $this->getID() ) {
1183 $u = new ViewCountUpdate( $this->getID() );
1184 array_push( $wgDeferredUpdateList, $u );
1185 $u = new SiteStatsUpdate( 1, 0, 0 );
1186 array_push( $wgDeferredUpdateList, $u );
1188 $u = new UserTalkUpdate( 0, $wgTitle->getNamespace(),
1189 $wgTitle->getDBkey() );
1190 array_push( $wgDeferredUpdateList, $u );
1194 # Do standard deferred updates after page edit.
1195 # Every 1000th edit, prune the recent changes table.
1197 /* private */ function editUpdates( $text )
1199 global $wgDeferredUpdateList, $wgTitle;
1202 if ( 0 == mt_rand( 0, 999 ) ) {
1203 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1204 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1207 $id = $this->getID();
1208 $title = $wgTitle->getPrefixedDBkey();
1209 $adj = $this->mCountAdjustment
;
1212 $u = new LinksUpdate( $id, $title );
1213 array_push( $wgDeferredUpdateList, $u );
1214 $u = new SiteStatsUpdate( 0, 1, $adj );
1215 array_push( $wgDeferredUpdateList, $u );
1216 $u = new SearchUpdate( $id, $title, $text );
1217 array_push( $wgDeferredUpdateList, $u );
1219 $u = new UserTalkUpdate( 1, $wgTitle->getNamespace(),
1220 $wgTitle->getDBkey() );
1221 array_push( $wgDeferredUpdateList, $u );
1225 /* private */ function setOldSubtitle()
1227 global $wgLang, $wgOut;
1229 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1230 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1231 $wgOut->setSubtitle( "({$r})" );
1234 # This function is called right before saving the wikitext,
1235 # so we can do things like signatures and links-in-context.
1237 function preSaveTransform( $text )
1240 while ( "" != $text ) {
1241 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1242 $s .= $this->pstPass2( $p[0] );
1244 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1246 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1247 $s .= "<nowiki>{$q[0]}</nowiki>";
1254 /* private */ function pstPass2( $text )
1256 global $wgUser, $wgLang, $wgTitle, $wgLocaltimezone;
1260 $n = $wgUser->getName();
1261 $k = $wgUser->getOption( "nickname" );
1262 if ( "" == $k ) { $k = $n; }
1263 if(isset($wgLocaltimezone)) {
1264 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1266 /* Note: this is an ugly timezone hack for the European wikis */
1267 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1268 " (" . date( "T" ) . ")";
1269 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1271 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1272 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1273 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1274 Namespace::getUser() ) . ":$n|$k]]", $text );
1276 # Context links: [[|name]] and [[name (context)|]]
1278 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1279 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1280 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1282 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1283 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1284 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1285 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1286 # [[ns:page (cont)|]]
1288 $t = $wgTitle->getText();
1289 if ( preg_match( $conpat, $t, $m ) ) {
1292 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1293 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1294 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1296 if ( "" == $context ) {
1297 $text = preg_replace( $p2, "[[\\1]]", $text );
1299 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1301 # Replace local image links with new [[image:]] style
1303 $text = preg_replace(
1304 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/upload\/" .
1305 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1306 "\\1[[image:\\3.\\4]]", $text );
1307 $text = preg_replace(
1308 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/images\/uploads\/" .
1309 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1310 "\\1[[image:\\3.\\4]]", $text );
1315 /* Caching functions */
1317 function tryFileCache() {
1320 if($this->isFileCacheable()) {
1321 $cache = new CacheManager( $wgTitle );
1322 if($cache->isFileCacheGood( $this->mTouched
)) {
1323 wfDebug( " tryFileCache() - about to load\n" );
1324 $cache->loadFromFileCache();
1327 wfDebug( " tryFileCache() - starting buffer\n" );
1328 if($cache->useGzip() && wfClientAcceptsGzip()) {
1329 /* For some reason, adding this header line over in
1330 CacheManager::saveToFileCache() fails on my test
1331 setup at home, though it works on the live install.
1332 Make double-sure... --brion */
1333 header( "Content-Encoding: gzip" );
1335 ob_start( array(&$cache, 'saveToFileCache' ) );
1338 wfDebug( " tryFileCache() - not cacheable\n" );
1342 function isFileCacheable() {
1343 global $wgUser, $wgTitle, $wgUseFileCache, $wgShowIPinHeader;
1344 global $action, $oldid, $diff, $redirect, $printable;
1345 return $wgUseFileCache
1346 and (!$wgShowIPinHeader)
1347 and ($wgUser->getId() == 0)
1348 and (!$wgUser->getNewtalk())
1349 and ($wgTitle->getNamespace
!= Namespace::getSpecial())
1350 and ($action == "view")
1351 and (!isset($oldid))
1353 and (!isset($redirect))
1354 and (!isset($printable))
1355 and (!$this->mRedirectedFrom
);